package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealServic;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

	@Autowired
	JedisPool jedisPool;

	@Reference
	SetmealServic setmealServic;

	@RequestMapping("/getToken")
	public String getToken(){

		String ak = "QpB4fUbPObhd0b0kI8QYtV9xAMkOaFJVN-Sd_yna";
		String sk = "VUhInOK9SLahdSRbVqpEb851_vJnDeVWHVSszQky";

		Auth auth = Auth.create(ak,sk);

		String token = auth.uploadToken("itcast-health363");

		System.out.println("token=="+token);

		return  token;
	}

	@RequestMapping("/add")
	public Result add(@RequestBody Setmeal setmeal , Integer[] checkgroupIds){

		log.debug("setmeal="+setmeal);
		log.debug("checkgroupIds="+checkgroupIds.toString());
		setmealServic.add(setmeal,checkgroupIds);
		log.debug("添加成功");

		return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
	}


	@RequestMapping("/findPage")
	public Result findPage(@RequestBody QueryPageBean queryPageBean){

		log.debug("queryPageBean="+queryPageBean);
		PageResult pageResult = setmealServic.findPage(queryPageBean);

		return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS,pageResult);
	}
	@RequestMapping("/saveImgName")
	public Result saveImgName(String imgName){

		log.debug("saveImgName=====:"+imgName);
		jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_RESOURCES,imgName);

		log.debug("将图片存储到redis:");

		return new Result(true, "图片存储到redis数据库成功");
	}




}
